package com.ycsj.goldmedalnewconcept.activity;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.CompoundButton.OnCheckedChangeListener;

import com.google.gson.Gson;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.FormEncodingBuilder;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.PrepareLessonsManagerActivity.MyAdapter;
import com.ycsj.goldmedalnewconcept.activity.PrepareLessonsManagerActivity.ViewHolder;
import com.ycsj.goldmedalnewconcept.bean.TeacherFansiResponse;
import com.ycsj.goldmedalnewconcept.bean.TeacherFansiResponse.FansiContent;
import com.ycsj.goldmedalnewconcept.constant.Constant;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;

//教师端 教学与反思 课程选择
//家长端 教学目标
public class PrepareLessonsActivity extends Activity implements OnClickListener {
	private GridView lv_preparelessons;
	private ImageView iv_back;
	private MyAdapter mAdapter;
	private TextView tv_titleprepare;
	private String title;
	private String class_id;
	private Intent intent;
	private RadioButton rb_1;
	private RadioButton rb_2;
	private RadioButton rb_3;
	private RadioButton rb_4;
	private int i = 0;
	private Handler mHandler;
	private String schoolId;
	private String account;
	private List<FansiContent> list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_preparelessons);

		mHandler = new Handler();
		intent = getIntent();
		title = getIntent().getStringExtra("title");
		class_id = getIntent().getStringExtra("class_id");
		list = new ArrayList<FansiContent>();
		
		schoolId = SPUtil.getString(this, "schoolId", "");
		account = SPUtil.getString(this, "account", "");
		initView();
		rb_1.setChecked(true);
		setOnListener();
		initDate();
		if("2".equals(SPUtil.getString(this, "role", ""))){
			http();
		}
	}

	private void http() {
		
		OkHttpClient mOkHttpClient = new OkHttpClient();
		// mOkHttpClient.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
		FormEncodingBuilder builder = new FormEncodingBuilder();
		builder.add("type", "22");
		builder.add("c", account);
		builder.add("role", "2");
		builder.add("schoolid", schoolId);
		builder.add("userclass", class_id);
		// http://192.168.0.198:8888/ycsj_platform/ManageMsg?type=22&c=13006314760&role=2&schoolid=0&userclass=1253
		Request request = new Request.Builder()
				.url(Constant.SERVER_LOC + "ManageMsg").post(builder.build())
				.build();
		mOkHttpClient.newCall(request).enqueue(new Callback() {

			@Override
			public void onFailure(Request arg0, IOException arg1) {
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						Toast.makeText(getBaseContext(), "网络无响应，请检查网络连接", 0)
								.show();
						mAdapter = new MyAdapter(PrepareLessonsActivity.this);
						lv_preparelessons.setAdapter(mAdapter);
					}
				});
			}

			@Override
			public void onResponse(Response arg0) throws IOException {
				// 解析Json数据，保存登录状态及身份信息

				Gson gson = new Gson();

				String json = arg0.body().string();
				TeacherFansiResponse fromJson = gson.fromJson(json,
						TeacherFansiResponse.class);
				Log.e("test", json);
				if(fromJson!=null){
					list = fromJson.list;
				}
				runOnUiThread(new Runnable() {
					@Override
					public void run() {
						mAdapter = new MyAdapter(PrepareLessonsActivity.this);
						lv_preparelessons.setAdapter(mAdapter);
					}
				});
			}
		});
	}

	private void initView() {
		lv_preparelessons = (GridView) findViewById(R.id.lv_preparelessons);
		tv_titleprepare = (TextView) findViewById(R.id.tv_titleprepare);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		rb_1 = (RadioButton) findViewById(R.id.rb_1);
		rb_2 = (RadioButton) findViewById(R.id.rb_2);
		rb_3 = (RadioButton) findViewById(R.id.rb_3);
		rb_4 = (RadioButton) findViewById(R.id.rb_4);
	}

	private void setOnListener() {
		iv_back.setOnClickListener(this);
		rb_1.setOnClickListener(this);
		rb_2.setOnClickListener(this);
		rb_3.setOnClickListener(this);
		rb_4.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.rb_1:
			i = 0;
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lv_preparelessons.setAdapter(mAdapter);
				}
			});

			break;
		case R.id.rb_2:
			i = 18;
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lv_preparelessons.setAdapter(mAdapter);
				}
			});
			break;
		case R.id.rb_3:
			i = 36;
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lv_preparelessons.setAdapter(mAdapter);
				}
			});
			break;
		case R.id.rb_4:
			i = 54;
			mHandler.post(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					lv_preparelessons.setAdapter(mAdapter);
				}
			});
			break;
		case R.id.iv_back:
			finish();
			break;
		}
	}

	private void initDate() {
		tv_titleprepare.setText(title);
		mAdapter = new MyAdapter(PrepareLessonsActivity.this);
		lv_preparelessons.setAdapter(mAdapter);
	}

	class ViewHolder {
//		private TextView tv_lessonodd;
//		private TextView tv_lessoneven;
		// private TextView tv_titleodd;
		// private TextView tv_titleeven;
		private TextView tv_unit;
//		private RelativeLayout ll_lessonodd;
//		private RelativeLayout ll_lessoneven;
	}

	class MyAdapter extends BaseAdapter {

		private LayoutInflater mInflater = null;

		private MyAdapter(Context context) {
			this.mInflater = LayoutInflater.from(context);
		}

		@Override
		public int getCount() {
			return 18;
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@SuppressWarnings("deprecation")
		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = mInflater.inflate(R.layout.list_prepare, null);
				// holder.img = (ImageView)item.findViewById(R.id.img)
//				holder.tv_lessoneven = (TextView) convertView
//						.findViewById(R.id.tv_lessoneven);
//				holder.tv_lessonodd = (TextView) convertView
//						.findViewById(R.id.tv_lessonodd);// 单数
//				holder.tv_newmsg1 = (TextView) convertView
//						.findViewById(R.id.tv_newmsg1);  // 单数
//				holder.tv_newmsg2 = (TextView) convertView
//						.findViewById(R.id.tv_newmsg2);
				// holder.tv_titleeven =
				// (TextView)convertView.findViewById(R.id.tv_titleeven);
				// holder.tv_titleodd =
				// (TextView)convertView.findViewById(R.id.tv_titleodd);//单数
				holder.tv_unit = (TextView) convertView
						.findViewById(R.id.tv_unit);
//				holder.ll_lessonodd = (RelativeLayout) convertView
//						.findViewById(R.id.ll_lessonodd);
//				holder.ll_lessoneven = (RelativeLayout) convertView
//						.findViewById(R.id.ll_lessoneven);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			holder.tv_unit.setText("Unit" + (position + i + 1));
//			holder.tv_lessonodd.setText("Lesson" + ((position + i) * 2 + 1));
//			holder.tv_lessoneven.setText("Lesson" + ((position + i) * 2 + 2));
//			holder.tv_newmsg1.setVisibility(holder.tv_newmsg1.INVISIBLE);
//			holder.tv_newmsg2.setVisibility(holder.tv_newmsg2.INVISIBLE);
//			holder.tv_lessonodd.setTextColor(PrepareLessonsActivity.this.getResources().getColor(R.color.title));
//			holder.tv_lessonodd.setBackground(PrepareLessonsActivity.this.getResources().getDrawable(R.drawable.lessonweidu_bg));
			holder.tv_unit.setTextColor(PrepareLessonsActivity.this.getResources().getColor(R.color.title));
			holder.tv_unit.setBackground(PrepareLessonsActivity.this.getResources().getDrawable(R.drawable.lessonweidu_bg));
			String odd = ((position + i) * 2 + 1)+"";
			String even =((position + i) * 2 + 2)+"";
			if(list!=null){
				  
			
			for (int i = 0; i < list.size(); i++) {
				if(odd.equals(list.get(i).lesson)){
					holder.tv_unit.setTextColor(PrepareLessonsActivity.this.getResources().getColor(R.color.white));
					holder.tv_unit.setBackground(PrepareLessonsActivity.this.getResources().getDrawable(R.drawable.shape_orange_score));
				}else if(even.equals(list.get(i).lesson)){
//					holder.tv_lessoneven.setTextColor(PrepareLessonsActivity.this.getResources().getColor(R.color.coffee));
//					holder.tv_lessoneven.setBackground(PrepareLessonsActivity.this.getResources().getDrawable(R.drawable.lessonyidu_bg));
//					holder.tv_unit.setTextColor(PrepareLessonsActivity.this.getResources().getColor(R.color.white));
//					holder.tv_unit.setBackground(PrepareLessonsActivity.this.getResources().getDrawable(R.drawable.shape_orange_score));
				}
			}
			}
			holder.tv_unit.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent1 = new Intent(PrepareLessonsActivity.this,
							TeachingObjectivesActivity.class);
					intent1.putExtra("Unit", position + i + 1);
					intent1.putExtra("Lesson", (position+ i) * 2  + 1);
					if (!"".equals(class_id)) {
						intent1.putExtra("class_id", class_id);
					}
					startActivity(intent1);
				}
			});

			return convertView;
		}
	}
}
